home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / bossa.arc / BOSS_SUP.ARC / HELP.C < prev    next >
C/C++ Source or Header  |  1990-03-12  |  9KB  |  258 lines

  1. /*
  2. ** HELP - Help driver for The Window Boss
  3. **
  4. ** Copyright (c) 1985 - Philip A. Mongelluzzo
  5. ** All rights reserved.
  6. **
  7. ** Comments:
  8. **
  9. **  Help files must be formatted properly, use GENINDEX to create the index
  10. **  file.
  11. **
  12. */
  13.  
  14. /* Header files */
  15.  
  16. #include "windows.h"
  17. #include "math.h"                       /* needed for atol */
  18.  
  19. /* Equates & definitions */
  20.  
  21. #define MAXKEY 255                      /* ISAM file length (# of keys) */
  22. #if AZTEC
  23. #define RD "r"
  24. #else
  25. #define RD "rb"                         /* file access mode */
  26. #endif
  27.  
  28. #undef  ESC
  29. #define K_PGUP 0x49                     /* a few scan codes */
  30. #define ESC    0x01
  31.  
  32. #if CI86                                /* Compiler specific stuff */
  33. #define atol atoi                       
  34. #endif
  35.  
  36. /* Function declarations */
  37.  
  38. extern FILE *fopen();                   /* Stream I/O */
  39. extern char *fgets();                   /* file line input */
  40. extern char *strcpy();                  /* string copy */
  41. extern char *strcat();                  /* concat function */
  42. extern long atol();                     /* ascii to integer */
  43. extern int fseek();                     /* DASD seek */
  44. extern long ftell();                    /* where are we DASD */
  45.  
  46. #if BORLAND | WATCOM | MSC
  47. int help_init(char *help_file_name);
  48. int help(char *subject);
  49. void showpage(long offset);
  50. int filbuf(long offset);
  51. #endif 
  52.  
  53. static struct list {                    /* master list structure */
  54.   char *key;                            /* pointer to %info string */
  55.   long loc;                             /* file offset */
  56.   int type;                             /* type <0=toc; 1=page; 2=menu> */
  57. } master[MAXKEY];                       /* master ISAM structure */
  58.  
  59. static char *scrbuf[26];                /* screen buffer (array of ptrs) */
  60. static char line[80];                   /* line buffer */
  61. static int cpg, eoh;                    /* clear screen, end of help flags */
  62. static FILE *fp;                        /* stream file pointer */
  63.  
  64. static WINDOWPTR wn1;                   /* window pointer */
  65. static int row, col;                    /* original cursor location */
  66.  
  67. /*
  68. *************
  69. * help_init *
  70. *************
  71. */
  72.  
  73. help_init(help_file_name)               /* init help function */
  74. char *help_file_name;
  75. {
  76.   if(!help("@"))                        /* allocate the memory */
  77.     return(FALSE);                      /* return if error */
  78.   return(help(help_file_name));         /* setup help ISAM */
  79. }
  80.  
  81. /*
  82. ********
  83. * help *
  84. ********
  85. */
  86.  
  87. help(subject)                           /* help function */
  88. char *subject;                          /* help subject request */
  89. {
  90. char tmpname[30];                       /* temporary filename */
  91. int i;                                  /* scratch integer */
  92. void showpage();                        /* display page function */
  93. int showmenu();                         /* display menu function */
  94. static initflg = FALSE;                 /* init complete flag */
  95.  
  96.   switch(subject[0]) {                  /* on first char of subject ... */
  97.     case '%':                           /* subject page */
  98.       wn1 = wn_open(0, 0, 0, 78, 23, BLUE<<4 | WHITE, BLUE<<4 | WHITE);
  99.       if(!wn1) return(FALSE);           /* error return */
  100.       wn_wrap(wn1,FALSE);               /* no wrap thank you */
  101.       v_rcpos(0, &row, &col);           /* remember current location */
  102.       v_hidec();                        /* hide the physical cursor */
  103.       wn_locate(wn1,0,0);               /* home virtual cursor */
  104.       if(!initflg) return(FALSE);       /* illegal sequence of calls */
  105.       i=0;                              /* start search index */
  106.       while(i < MAXKEY) {               /* look for match in key table */
  107.         if(!strncmp(subject,master[i].key,strlen(subject))) {
  108.           showpage(master[i].loc);      /* display all pages */
  109.           return(TRUE);                 /* return .. all is well */
  110.         }
  111.         i++;
  112.       }
  113.       wn_printf(wn1," Sorry... No info on %s\n", subject);
  114.       wn_printf(wn1," Press any key to continue...");
  115.       v_getch();
  116.       wn_close(wn1);
  117.       return(FALSE);                    /* no match !! */
  118.     case '$':                           /* menu page */
  119.       break;                            /* sometime in the future */
  120.     case '@':                           /* Initialize (allocate memory) */
  121.       for(i=0; i<MAXKEY; i++) {
  122.         master[i].key = malloc(25);     /* keys can be 25 chars long */
  123.         if(!master[i].key) return(FALSE);
  124.       }
  125.       for(i=0; i<25; i++) {             /* allocate memory for screen */
  126.         scrbuf[i] =  malloc(81);        /* buffer */
  127.         if(!scrbuf[i]) return(FALSE);
  128.       }
  129.       initflg = TRUE;
  130.       return(TRUE);
  131.     default:                            /* assume help filename */
  132.       if(!initflg) return(FALSE);       /* illegal sequence of calls */
  133.       strcpy(tmpname,subject);          /* form filename from root */
  134.       strcat(tmpname,".ndx");
  135.       if(!(fp = fopen(tmpname,RD))) {   /* index file cant be found ! */
  136.         printf("Can't find index file\n");
  137.         printf("Press any key to continue...");
  138.         v_getch();
  139.         return(FALSE);
  140.       }
  141.       i = 0;                            /* read and process key file */
  142.       while(fgets(line,80,fp)) {        /* build the master index table */
  143.         strcpy(master[i].key, line);
  144.         if(line[0] != '%')              /* ignore all but subects */
  145.           continue;
  146.         fgets(line,80,fp);
  147.         master[i].loc = atol(line);
  148.         i++;
  149.       }
  150.       fclose(fp);                       /* close index file */
  151.       strcpy(tmpname, subject);         /* form help file name from root */
  152.       strcat(tmpname, ".hlp");          /* open and leave open ! */
  153.       if(!(fp = fopen(tmpname, RD))) return(FALSE);
  154.       return(TRUE);
  155.   }
  156. }
  157.  
  158. /*
  159. ************
  160. * showpage *
  161. ************
  162. */
  163.  
  164. void showpage(offset)                   /* display page function */
  165. long offset;
  166. {
  167. int i,j;                                /* scratch integers */
  168. long pages[MAXKEY];                     /* stack of pages */
  169. int pndx;                               /* stack pointer */
  170. int keyflg;                             /* vaild key flag */
  171.  
  172.   pndx = 0;                             /* init stack pointer */
  173.   pages[0] = offset;                    /* initial page */
  174.  
  175.   while(TRUE) {                         /* till forever ... */
  176.     i = filbuf(offset);                 /* fill the buffer */
  177.     for(j=0; j<i; j++)                  /* display buffer */
  178.       wn_puts(wn1,j,0,scrbuf[j]);
  179.     wn1->style |= BOLD;                 /* toggle bold on */
  180.     if(eoh) 
  181.       wn_puts(wn1, 22, 0, " End of help, PgUp for previous screen, any other key to continue...");
  182.     else
  183.       wn_puts(wn1, 22, 0, " Esc to quit help, PgUp for previous screen, any other key to continue...");
  184.     wn1->style ^= BOLD;                 /* toggle bold off */
  185.     keyflg = FALSE;                     /* valid key flag */
  186.     do {                                /* loop till vaild key */
  187.       switch (v_getch() >> 8) {         /* based on user input */
  188.         case K_PGUP:                    /* do all the right things */
  189.           pndx--;
  190.           if(pndx <0) {
  191.             wn_close(wn1);
  192.             v_locate(0, row, col);
  193.             return;
  194.           }
  195.           offset = pages[pndx];
  196.           keyflg = TRUE;
  197.           eoh = FALSE;                  /* force clear screen */
  198.           cpg = TRUE;                   /* and cancel end of help flag */
  199.           break;
  200.         case ESC:
  201.           wn_close(wn1);
  202.           v_locate(0, row, col);
  203.           return;
  204.         default:
  205.           offset = pages[++pndx] = ftell(fp);
  206.           keyflg = TRUE;
  207.           break;
  208.       }
  209.     } while (!keyflg);                  /* loop till valid key */
  210.     if(cpg) wn_clr(wn1);
  211.     if(eoh) {
  212.       wn_close(wn1);
  213.       v_locate(0, row, col);
  214.       return;
  215.     }
  216.   }
  217. }
  218.  
  219. /*
  220. **********
  221. * filbuf *
  222. **********
  223. */
  224. filbuf(offset)                          /* fill screen buffer */
  225. long offset;
  226. {
  227. int i;
  228. char *p, *p1;
  229.  
  230.   i=0;                                  /* initialize */
  231.   cpg = eoh = FALSE;                    /* assume nothing */
  232.   fseek(fp, offset, 0);                 /* position file */
  233.   do {
  234.     fgets(line, 80, fp);                /* fetch a line */
  235.       cpg = !strncmp(".cp", line,3);  /* lite clear screen and */
  236.       eoh = !strncmp("*END*\n",line,5); /* end of help flags */
  237.       if(cpg || eoh) break;
  238.       p = &line[0];                     /* a long way around to */
  239.       p1 = scrbuf[i];                   /* filter the carriage */
  240.       while (*p) {                      /* returns !! */
  241.         switch (*p) {
  242.           case '\r':
  243.           case '\n':
  244.             p++;
  245.             break;
  246.           default:
  247.             *p1++ = *p++;
  248.             break;
  249.         }
  250.       }
  251.       *p1 = '\0';
  252.       i++;
  253.   } while (TRUE);
  254.   return(i);                            /* return the # of lines on page */
  255. }
  256.  
  257. /* End */
  258.